<?php
/**
 * Created by PhpStorm.
 * User: lih
 * Date: 2018/5/19
 * Time: 21:39
 */

namespace common\models;

use Yii;
use yii\db\Query;
use common\models\User;
use common\utils\Utils;

/**
 * Class ForumPost
 * @package common\models
 * @property string $fid
 * @property string $pid
 * @property string $authorid
 * @property string $author
 * @property string $subject
 * @property string $message
 * @property \common\models\PostMedia $mediaextra
 * @property string $displayorder
 * @property string $views
 * @property string $replies
 * @property string $dateline
 * @property string $lastpost
 */
class ForumPost
{

    public $fid;
    public $pid;
    public $authorid;
    public $author;
    public $subject;
    public $message;
    public $mediaextra;
    public $displayorder;
    public $views;
    public $replies;
    public $dateline;
    public $lastpost;

    /**
     * @return array
     */
    public function insertPost()
    {
        $returnArray = array();

        $tableNameThread = Utils::getDzTableName('forum_thread');
        $tableNamePost = Utils::getDzTableName('forum_post');
        $tableForumPostTableid = Utils::getDzTableName('forum_post_tableid');

        try {
            $dbConn = Yii::$app->db;

            $timenow = time();
            $strMediaExtra = $this->mediaextra !== null ? $this->mediaextra->toJson() : '';
            if(!Utils::isEmpty($this->dateline)) {
                $timenow = $this->dateline;
            }
            //insert into forum_thread
            $dbConn->createCommand()
                ->insert($tableNameThread, [
                    'fid' => $this->fid,
                    'author' => $this->author,
                    'authorid' => $this->authorid,
                    'subject' => $this->subject,
                    'mediaextra' => $strMediaExtra,
                    'dateline' => $timenow,
                    'lastpost' => $timenow,
                ])
                ->execute();

            $insertTid = $dbConn->getLastInsertID();

            $returnArray['pid'] = $insertTid;

            $dbConn->createCommand()
                ->insert($tableForumPostTableid, [
                    'pid' => '',
                ])
                ->execute();

            $insertPid = $dbConn->getLastInsertID();

            //insert into forum_post
            $dbConn->createCommand()
                ->insert($tableNamePost, [
                    'pid' => $insertPid,
                    'tid' => $insertTid,
                    'fid' => $this->fid,
                    'first' => '1',
                    'author' => $this->author,
                    'authorid' => $this->authorid,
                    'subject' => $this->subject,
                    'dateline' => $timenow,
                ])
                ->execute();

        } catch(\Exception $e) {
        }

        return $returnArray;
    }

    /**
     * @param $postData array
     */
    public function fromArray($dataArray)
    {
        if(!empty($dataArray)) {
            $this->fid = @$dataArray['fid'];
            $this->authorid = @$dataArray['authorid'];
            $this->author = @$dataArray['author'];
            $this->subject = @$dataArray['subject'];
            $this->dateline = @$dataArray['dateline'];
            if(!empty($dataArray['mediaextra'])) {
                $strMediaextra = json_encode($dataArray['mediaextra'], JSON_UNESCAPED_UNICODE);
                $postMedia = new PostMedia();
                $postMedia->fromJson($strMediaextra);
                $this->mediaextra = $postMedia;
            }
        }
    }

}